package com.color.admin.common.config;


import com.color.admin.common.core.base.BaseConfig;
import com.color.admin.common.core.base.RedisConfig;
import com.color.admin.common.core.utils.cache.RedisUtils;
import com.color.admin.common.swagger.config.SpringDocConfig;
import com.color.admin.files.service.entity.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author HaiQiu
 * 微服务模块配置
 */
@Configuration
@EnableJpaAuditing
public class AppConfig extends SpringDocConfig {

    @Autowired
    SysFile sysFile;


    /**
     * 基类方法
     * @return 拦截器，jpa实现注解设置创建人，更新人
     */
    @Bean
    public BaseConfig baseConfig(){
        return new BaseConfig();
    }


    /**
     * 开放静态资源
     *
     * @return 映射
     */
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler(sysFile.getSuffix() + "**").
                        addResourceLocations("file:" + sysFile.getStorage());
            }
        };
    }

    /**
     * 注入redis序列化
     * @param lettuceConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){
        return new RedisConfig().redisTemplate(lettuceConnectionFactory);
    }

    /**
     * 注入redis工具类
     * @return redis工具类
     */
    @Bean
    public RedisUtils redisUtils(){
        return new RedisUtils();
    }

}
